package com.git.util;

import java.util.Properties;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import com.alibaba.druid.util.DruidPasswordCallback;

public class DBPasswordCallback extends DruidPasswordCallback {

	public void setProperties(Properties properties) {
		super.setProperties(properties);
		String pwd = properties.getProperty("password");
		if (StringUtils.isNotBlank(pwd)) {
			try {
				// 这里的password是将jdbc.properties配置得到的密码进行解密之后的值
				 setPassword(new String(Base64.decodeBase64(pwd.getBytes())).toCharArray());
			} catch (Exception e) {
				setPassword(pwd.toCharArray());
			}
		}
	}

	// 请使用该方法加密后，把密文写入classpath:/config/jdbc.properties
	public static void main(String[] args) {
		String str = "123456";
		byte[] tmp = Base64.encodeBase64(str.getBytes(), true);
		String strEn = new String(tmp);
		System.out.println(strEn);
	}
}